package com.gitee.aurora.core.rule.dao;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gitee.aurora.core.comm.AuroraJdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.Types;
import java.util.Map;

@Component
public class RuleDao {

    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;


    public Map<String, Object> getRule(JdbcTemplate jdbcTemplate, String tableName, String deptCode) throws JsonProcessingException {

        String sql = "SELECT ID,TABLE_NAME,RULE FROM TAB_RULE WHERE TABLE_NAME = ? AND DEPT_CODE = ?";
        int[] types = new int[]{Types.VARCHAR, Types.VARCHAR};
        return jdbcTemplate.queryForMap(sql, new Object[]{tableName, deptCode}, types);
    }

    public void saveRule(JdbcTemplate jdbcTemplate, String tableName, String deptCode, String ruleStr) {
        System.out.println(tableName);
        System.out.println(ruleStr);
        String sql = "insert into tab_rule(table_name,dept_code,rule) values (?,?,?)";
        int[] types = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
        Object[] object = new Object[]{tableName, deptCode, ruleStr};
        jdbcTemplate.update(sql, object, types);
    }

}
